﻿namespace System.Windows.Media.Imaging
{
    internal static class WriteableBitmapExtensions
    {
        // Fields
        private const int SizeOfArgb = 4;
        private const float StepFactor = 2f;

        // Methods
        public static void Clear(this WriteableBitmap bmp)
        {
            Array.Clear(bmp.Pixels, 0, bmp.Pixels.Length);
        }

        public static void Clear(this WriteableBitmap bmp, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            int[] pixels = bmp.Pixels;
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int num5 = pixelWidth * 4;
            for (int i = 0; i < pixelWidth; i++)
            {
                pixels[i] = num2;
            }
            int num7 = 1;
            int num8 = 1;
            while (num8 < pixelHeight)
            {
                Buffer.BlockCopy(pixels, 0, pixels, num8 * num5, num7 * num5);
                num8 += num7;
                num7 = Math.Min((int)(2 * num7), (int)(pixelHeight - num8));
            }
        }

        public static void DrawEllipse(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
        {
            int xr = (x2 - x1) >> 1;
            int yr = (y2 - y1) >> 1;
            int xc = x1 + xr;
            int yc = y1 + yr;
            bmp.DrawEllipseCentered(xc, yc, xr, yr, color);
        }

        public static void DrawEllipse(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawEllipse(x1, y1, x2, y2, num2);
        }

        public static void DrawEllipseCentered(this WriteableBitmap bmp, int xc, int yc, int xr, int yr, int color)
        {
            int num3;
            int num4;
            int num5;
            int num6;
            int num7;
            int num8;
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            int num9 = xr;
            int num10 = 0;
            int num11 = (xr * xr) << 1;
            int num12 = (yr * yr) << 1;
            int num13 = (yr * yr) * (1 - (xr << 1));
            int num14 = xr * xr;
            int num15 = 0;
            int num16 = num12 * xr;
            int num17 = 0;
            while (num16 >= num17)
            {
                num5 = yc + num10;
                num6 = yc - num10;
                if (num5 < 0)
                {
                    num5 = 0;
                }
                if (num5 >= pixelHeight)
                {
                    num5 = pixelHeight - 1;
                }
                if (num6 < 0)
                {
                    num6 = 0;
                }
                if (num6 >= pixelHeight)
                {
                    num6 = pixelHeight - 1;
                }
                num3 = num5 * pixelWidth;
                num4 = num6 * pixelWidth;
                num8 = xc + num9;
                num7 = xc - num9;
                if (num8 < 0)
                {
                    num8 = 0;
                }
                if (num8 >= pixelWidth)
                {
                    num8 = pixelWidth - 1;
                }
                if (num7 < 0)
                {
                    num7 = 0;
                }
                if (num7 >= pixelWidth)
                {
                    num7 = pixelWidth - 1;
                }
                pixels[num8 + num3] = color;
                pixels[num7 + num3] = color;
                pixels[num7 + num4] = color;
                pixels[num8 + num4] = color;
                num10++;
                num17 += num11;
                num15 += num14;
                num14 += num11;
                if ((num13 + (num15 << 1)) > 0)
                {
                    num9--;
                    num16 -= num12;
                    num15 += num13;
                    num13 += num12;
                }
            }
            num9 = 0;
            num10 = yr;
            num5 = yc + num10;
            num6 = yc - num10;
            if (num5 < 0)
            {
                num5 = 0;
            }
            if (num5 >= pixelHeight)
            {
                num5 = pixelHeight - 1;
            }
            if (num6 < 0)
            {
                num6 = 0;
            }
            if (num6 >= pixelHeight)
            {
                num6 = pixelHeight - 1;
            }
            num3 = num5 * pixelWidth;
            num4 = num6 * pixelWidth;
            num13 = yr * yr;
            num14 = (xr * xr) * (1 - (yr << 1));
            num15 = 0;
            num16 = 0;
            num17 = num11 * yr;
            while (num16 <= num17)
            {
                num8 = xc + num9;
                num7 = xc - num9;
                if (num8 < 0)
                {
                    num8 = 0;
                }
                if (num8 >= pixelWidth)
                {
                    num8 = pixelWidth - 1;
                }
                if (num7 < 0)
                {
                    num7 = 0;
                }
                if (num7 >= pixelWidth)
                {
                    num7 = pixelWidth - 1;
                }
                pixels[num8 + num3] = color;
                pixels[num7 + num3] = color;
                pixels[num7 + num4] = color;
                pixels[num8 + num4] = color;
                num9++;
                num16 += num12;
                num15 += num13;
                num13 += num12;
                if ((num14 + (num15 << 1)) > 0)
                {
                    num10--;
                    num5 = yc + num10;
                    num6 = yc - num10;
                    if (num5 < 0)
                    {
                        num5 = 0;
                    }
                    if (num5 >= pixelHeight)
                    {
                        num5 = pixelHeight - 1;
                    }
                    if (num6 < 0)
                    {
                        num6 = 0;
                    }
                    if (num6 >= pixelHeight)
                    {
                        num6 = pixelHeight - 1;
                    }
                    num3 = num5 * pixelWidth;
                    num4 = num6 * pixelWidth;
                    num17 -= num11;
                    num15 += num14;
                    num14 += num11;
                }
            }
        }

        public static void DrawEllipseCentered(this WriteableBitmap bmp, int xc, int yc, int xr, int yr, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawEllipseCentered(xc, yc, xr, yr, num2);
        }

        public static void DrawLine(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
        {
            DrawLine(bmp.Pixels, bmp.PixelWidth, bmp.PixelHeight, x1, y1, x2, y2, color);
        }

        public static void DrawLine(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawLine(x1, y1, x2, y2, num2);
        }

        public static void DrawLine(int[] pixels, int pixelWidth, int pixelHeight, int x1, int y1, int x2, int y2, int color)
        {
            int num3;
            int num4;
            int num = x2 - x1;
            int num2 = y2 - y1;
            if (num2 >= 0)
            {
                num4 = num2;
            }
            else
            {
                num4 = -num2;
            }
            if (num >= 0)
            {
                num3 = num;
            }
            else
            {
                num3 = -num;
            }
            if (num3 > num4)
            {
                if (num < 0)
                {
                    int num5 = x1;
                    x1 = x2;
                    x2 = num5;
                    num5 = y1;
                    y1 = y2;
                    y2 = num5;
                }
                int num6 = (num2 << 8) / num;
                int num7 = y1 << 8;
                int num8 = y2 << 8;
                int num9 = pixelHeight << 8;
                if (y1 < y2)
                {
                    if ((y1 >= pixelHeight) || (y2 < 0))
                    {
                        return;
                    }
                    if (num7 < 0)
                    {
                        if (num6 == 0)
                        {
                            return;
                        }
                        int num10 = num7;
                        num7 = (num6 - 1) + ((num7 + 1) % num6);
                        x1 += (num7 - num10) / num6;
                    }
                    if ((num8 >= num9) && (num6 != 0))
                    {
                        num8 = (num9 - 1) - (((num9 - 1) - num7) % num6);
                        x2 = x1 + ((num8 - num7) / num6);
                    }
                }
                else
                {
                    if ((y2 >= pixelHeight) || (y1 < 0))
                    {
                        return;
                    }
                    if (num7 >= num9)
                    {
                        if (num6 == 0)
                        {
                            return;
                        }
                        int num11 = num7;
                        num7 = (num9 - 1) + (num6 - (((num9 - 1) - num11) % num6));
                        x1 += (num7 - num11) / num6;
                    }
                    if ((num8 < 0) && (num6 != 0))
                    {
                        num8 = num7 % num6;
                        x2 = x1 + ((num8 - num7) / num6);
                    }
                }
                if (x1 < 0)
                {
                    num7 -= num6 * x1;
                    x1 = 0;
                }
                if (x2 >= pixelWidth)
                {
                    x2 = pixelWidth - 1;
                }
                int num12 = num7;
                int num13 = num12 >> 8;
                int num14 = num13;
                int index = x1 + (num13 * pixelWidth);
                int num16 = (num6 < 0) ? (1 - pixelWidth) : (1 + pixelWidth);
                for (int i = x1; i <= x2; i++)
                {
                    if (index < pixels.Length)
                    {
                        pixels[index] = color;
                    }
                    num12 += num6;
                    num13 = num12 >> 8;
                    if (num13 != num14)
                    {
                        num14 = num13;
                        index += num16;
                    }
                    else
                    {
                        index++;
                    }
                }
            }
            else if (num4 != 0)
            {
                if (num2 < 0)
                {
                    int num18 = x1;
                    x1 = x2;
                    x2 = num18;
                    num18 = y1;
                    y1 = y2;
                    y2 = num18;
                }
                int num19 = x1 << 8;
                int num20 = x2 << 8;
                int num21 = pixelWidth << 8;
                int num22 = (num << 8) / num2;
                if (x1 < x2)
                {
                    if ((x1 >= pixelWidth) || (x2 < 0))
                    {
                        return;
                    }
                    if (num19 < 0)
                    {
                        if (num22 == 0)
                        {
                            return;
                        }
                        int num23 = num19;
                        num19 = (num22 - 1) + ((num19 + 1) % num22);
                        y1 += (num19 - num23) / num22;
                    }
                    if ((num20 >= num21) && (num22 != 0))
                    {
                        num20 = (num21 - 1) - (((num21 - 1) - num19) % num22);
                        y2 = y1 + ((num20 - num19) / num22);
                    }
                }
                else
                {
                    if ((x2 >= pixelWidth) || (x1 < 0))
                    {
                        return;
                    }
                    if (num19 >= num21)
                    {
                        if (num22 == 0)
                        {
                            return;
                        }
                        int num24 = num19;
                        num19 = (num21 - 1) + (num22 - (((num21 - 1) - num24) % num22));
                        y1 += (num19 - num24) / num22;
                    }
                    if ((num20 < 0) && (num22 != 0))
                    {
                        num20 = num19 % num22;
                        y2 = y1 + ((num20 - num19) / num22);
                    }
                }
                if (y1 < 0)
                {
                    num19 -= num22 * y1;
                    y1 = 0;
                }
                if (y2 >= pixelHeight)
                {
                    y2 = pixelHeight - 1;
                }
                int num25 = num19 + ((y1 * pixelWidth) << 8);
                int num26 = (pixelWidth << 8) + num22;
                for (int j = y1; j <= y2; j++)
                {
                    int num28 = num25 >> 8;
                    if (num28 < pixels.Length)
                    {
                        pixels[num28] = color;
                    }
                    num25 += num26;
                }
            }
        }

        public static void DrawLineBresenham(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
        {
            int num7;
            int num8;
            int num9;
            int num10;
            int num11;
            int num12;
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            int num3 = x2 - x1;
            int num4 = y2 - y1;
            int num5 = 0;
            if (num3 < 0)
            {
                num3 = -num3;
                num5 = -1;
            }
            else if (num3 > 0)
            {
                num5 = 1;
            }
            int num6 = 0;
            if (num4 < 0)
            {
                num4 = -num4;
                num6 = -1;
            }
            else if (num4 > 0)
            {
                num6 = 1;
            }
            if (num3 > num4)
            {
                num7 = num5;
                num8 = 0;
                num9 = num5;
                num10 = num6;
                num11 = num4;
                num12 = num3;
            }
            else
            {
                num7 = 0;
                num8 = num6;
                num9 = num5;
                num10 = num6;
                num11 = num3;
                num12 = num4;
            }
            int num13 = x1;
            int num14 = y1;
            int num15 = num12 >> 1;
            if (((num14 < pixelHeight) && (num14 >= 0)) && ((num13 < pixelWidth) && (num13 >= 0)))
            {
                pixels[(num14 * pixelWidth) + num13] = color;
            }
            for (int i = 0; i < num12; i++)
            {
                num15 -= num11;
                if (num15 < 0)
                {
                    num15 += num12;
                    num13 += num9;
                    num14 += num10;
                }
                else
                {
                    num13 += num7;
                    num14 += num8;
                }
                if (((num14 < pixelHeight) && (num14 >= 0)) && ((num13 < pixelWidth) && (num13 >= 0)))
                {
                    pixels[(num14 * pixelWidth) + num13] = color;
                }
            }
        }

        public static void DrawLineBresenham(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawLineBresenham(x1, y1, x2, y2, num2);
        }

        public static void DrawLineDDA(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
        {
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            int num3 = x2 - x1;
            int num4 = y2 - y1;
            int num5 = (num4 >= 0) ? num4 : -num4;
            int num6 = (num3 >= 0) ? num3 : -num3;
            if (num6 > num5)
            {
                num5 = num6;
            }
            if (num5 != 0)
            {
                float num7 = ((float)num3) / ((float)num5);
                float num8 = ((float)num4) / ((float)num5);
                float num9 = x1;
                float num10 = y1;
                for (int i = 0; i < num5; i++)
                {
                    if (((num10 < pixelHeight) && (num10 >= 0f)) && ((num9 < pixelWidth) && (num9 >= 0f)))
                    {
                        pixels[(((int)num10) * pixelWidth) + ((int)num9)] = color;
                    }
                    num9 += num7;
                    num10 += num8;
                }
            }
        }

        public static void DrawLineDDA(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawLineDDA(x1, y1, x2, y2, num2);
        }

        public static void DrawPolyline(this WriteableBitmap bmp, int[] points, int color)
        {
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            int num3 = points[0];
            int num4 = points[1];
            int length = points.Length;
            for (int i = 2; i < length; i += 2)
            {
                int num5 = points[i];
                int num6 = points[i + 1];
                DrawLine(pixels, pixelWidth, pixelHeight, num3, num4, num5, num6, color);
                num3 = num5;
                num4 = num6;
            }
        }

        public static void DrawPolyline(this WriteableBitmap bmp, int[] points, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawPolyline(points, num2);
        }

        public static void DrawPolyline2(this WriteableBitmap bmp, int[] points, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            int length = points.Length;
            for (int i = 0; i < length; i += 4)
            {
                int num5 = points[i];
                int num6 = points[i + 1];
                int num7 = points[i + 2];
                int num8 = points[i + 3];
                DrawLine(pixels, pixelWidth, pixelHeight, num5, num6, num7, num8, num2);
            }
        }

        public static void DrawQuad(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int color)
        {
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            DrawLine(pixels, pixelWidth, pixelHeight, x1, y1, x2, y2, color);
            DrawLine(pixels, pixelWidth, pixelHeight, x2, y2, x3, y3, color);
            DrawLine(pixels, pixelWidth, pixelHeight, x3, y3, x4, y4, color);
            DrawLine(pixels, pixelWidth, pixelHeight, x4, y4, x1, y1, color);
        }

        public static void DrawQuad(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawQuad(x1, y1, x2, y2, x3, y3, x4, y4, num2);
        }

        public static void DrawRectangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
        {
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            if (((((x1 >= 0) || (x2 >= 0)) && ((x1 < pixelWidth) || (x2 < pixelWidth))) && ((y1 >= 0) || (y2 >= 0))) && ((y1 < pixelHeight) || (y2 < pixelHeight)))
            {
                if (x1 < 0)
                {
                    x1 = 0;
                }
                if (y1 < 0)
                {
                    y1 = 0;
                }
                if (x2 < 0)
                {
                    x2 = 0;
                }
                if (y2 < 0)
                {
                    y2 = 0;
                }
                if (x1 >= pixelWidth)
                {
                    x1 = pixelWidth - 1;
                }
                if (y1 >= pixelHeight)
                {
                    y1 = pixelHeight - 1;
                }
                if (x2 >= pixelWidth)
                {
                    x2 = pixelWidth - 1;
                }
                if (y2 >= pixelHeight)
                {
                    y2 = pixelHeight - 1;
                }
                int num3 = y1 * pixelWidth;
                int num4 = y2 * pixelWidth;
                int index = (num4 - pixelWidth) + x1;
                int num6 = num3 + x2;
                int num7 = num3 + x1;
                for (int i = num7; i < num6; i++)
                {
                    pixels[i] = color;
                    if (index >= 0)
                    {
                        pixels[index] = color;
                    }
                    index++;
                }
                num6 = num7 + pixelWidth;
                index -= pixelWidth;
                for (int j = ((num3 + x2) - 1) + pixelWidth; j < index; j += pixelWidth)
                {
                    pixels[j] = color;
                    pixels[num6] = color;
                    num6 += pixelWidth;
                }
            }
        }

        public static void DrawRectangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawRectangle(x1, y1, x2, y2, num2);
        }

        public static void DrawTriangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int x3, int y3, int color)
        {
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            DrawLine(pixels, pixelWidth, pixelHeight, x1, y1, x2, y2, color);
            DrawLine(pixels, pixelWidth, pixelHeight, x2, y2, x3, y3, color);
            DrawLine(pixels, pixelWidth, pixelHeight, x3, y3, x1, y1, color);
        }

        public static void DrawTriangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int x3, int y3, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.DrawTriangle(x1, y1, x2, y2, x3, y3, num2);
        }

        public static void FillEllipse(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
        {
            int xr = (x2 - x1) >> 1;
            int yr = (y2 - y1) >> 1;
            int xc = x1 + xr;
            int yc = y1 + yr;
            bmp.FillEllipseCentered(xc, yc, xr, yr, color);
        }

        public static void FillEllipse(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.FillEllipse(x1, y1, x2, y2, num2);
        }

        public static void FillEllipseCentered(this WriteableBitmap bmp, int xc, int yc, int xr, int yr, int color)
        {
            int num3;
            int num4;
            int num5;
            int num6;
            int num7;
            int num8;
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            int num9 = xr;
            int num10 = 0;
            int num11 = (xr * xr) << 1;
            int num12 = (yr * yr) << 1;
            int num13 = (yr * yr) * (1 - (xr << 1));
            int num14 = xr * xr;
            int num15 = 0;
            int num16 = num12 * xr;
            int num17 = 0;
            while (num16 >= num17)
            {
                num5 = yc + num10;
                num6 = yc - num10;
                if (num5 < 0)
                {
                    num5 = 0;
                }
                if (num5 >= pixelHeight)
                {
                    num5 = pixelHeight - 1;
                }
                if (num6 < 0)
                {
                    num6 = 0;
                }
                if (num6 >= pixelHeight)
                {
                    num6 = pixelHeight - 1;
                }
                num3 = num5 * pixelWidth;
                num4 = num6 * pixelWidth;
                num8 = xc + num9;
                num7 = xc - num9;
                if (num8 < 0)
                {
                    num8 = 0;
                }
                if (num8 >= pixelWidth)
                {
                    num8 = pixelWidth - 1;
                }
                if (num7 < 0)
                {
                    num7 = 0;
                }
                if (num7 >= pixelWidth)
                {
                    num7 = pixelWidth - 1;
                }
                for (int i = num7; i <= num8; i++)
                {
                    pixels[i + num3] = color;
                    pixels[i + num4] = color;
                }
                num10++;
                num17 += num11;
                num15 += num14;
                num14 += num11;
                if ((num13 + (num15 << 1)) > 0)
                {
                    num9--;
                    num16 -= num12;
                    num15 += num13;
                    num13 += num12;
                }
            }
            num9 = 0;
            num10 = yr;
            num5 = yc + num10;
            num6 = yc - num10;
            if (num5 < 0)
            {
                num5 = 0;
            }
            if (num5 >= pixelHeight)
            {
                num5 = pixelHeight - 1;
            }
            if (num6 < 0)
            {
                num6 = 0;
            }
            if (num6 >= pixelHeight)
            {
                num6 = pixelHeight - 1;
            }
            num3 = num5 * pixelWidth;
            num4 = num6 * pixelWidth;
            num13 = yr * yr;
            num14 = (xr * xr) * (1 - (yr << 1));
            num15 = 0;
            num16 = 0;
            num17 = num11 * yr;
            while (num16 <= num17)
            {
                num8 = xc + num9;
                num7 = xc - num9;
                if (num8 < 0)
                {
                    num8 = 0;
                }
                if (num8 >= pixelWidth)
                {
                    num8 = pixelWidth - 1;
                }
                if (num7 < 0)
                {
                    num7 = 0;
                }
                if (num7 >= pixelWidth)
                {
                    num7 = pixelWidth - 1;
                }
                for (int j = num7; j <= num8; j++)
                {
                    pixels[j + num3] = color;
                    pixels[j + num4] = color;
                }
                num9++;
                num16 += num12;
                num15 += num13;
                num13 += num12;
                if ((num14 + (num15 << 1)) > 0)
                {
                    num10--;
                    num5 = yc + num10;
                    num6 = yc - num10;
                    if (num5 < 0)
                    {
                        num5 = 0;
                    }
                    if (num5 >= pixelHeight)
                    {
                        num5 = pixelHeight - 1;
                    }
                    if (num6 < 0)
                    {
                        num6 = 0;
                    }
                    if (num6 >= pixelHeight)
                    {
                        num6 = pixelHeight - 1;
                    }
                    num3 = num5 * pixelWidth;
                    num4 = num6 * pixelWidth;
                    num17 -= num11;
                    num15 += num14;
                    num14 += num11;
                }
            }
        }

        public static void FillEllipseCentered(this WriteableBitmap bmp, int xc, int yc, int xr, int yr, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.FillEllipseCentered(xc, yc, xr, yr, num2);
        }

        public static void FillPolygon(this WriteableBitmap bmp, int[] points, int color)
        {
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            int length = points.Length;
            int num4 = points.Length >> 1;
            int[] numArray2 = new int[num4];
            int num5 = pixelHeight;
            int num6 = 0;
            for (int i = 1; i < length; i += 2)
            {
                int num8 = points[i];
                if (num8 < num5)
                {
                    num5 = num8;
                }
                if (num8 > num6)
                {
                    num6 = num8;
                }
            }
            if (num5 < 0)
            {
                num5 = 0;
            }
            if (num6 >= pixelHeight)
            {
                num6 = pixelHeight - 1;
            }
            for (int j = num5; j <= num6; j++)
            {
                float num10 = points[0];
                float num11 = points[1];
                int num12 = 0;
                for (int k = 2; k < length; k += 2)
                {
                    float num14 = points[k];
                    float num15 = points[k + 1];
                    if (((num11 < j) && (num15 >= j)) || ((num15 < j) && (num11 >= j)))
                    {
                        numArray2[num12++] = (int)(num10 + (((j - num11) / (num15 - num11)) * (num14 - num10)));
                    }
                    num10 = num14;
                    num11 = num15;
                }
                for (int m = 1; m < num12; m++)
                {
                    int num16 = numArray2[m];
                    int index = m;
                    while ((index > 0) && (numArray2[index - 1] > num16))
                    {
                        numArray2[index] = numArray2[index - 1];
                        index--;
                    }
                    numArray2[index] = num16;
                }
                for (int n = 0; n < (num12 - 1); n += 2)
                {
                    int num20 = numArray2[n];
                    int num21 = numArray2[n + 1];
                    if ((num21 > 0) && (num20 < pixelWidth))
                    {
                        if (num20 < 0)
                        {
                            num20 = 0;
                        }
                        if (num21 >= pixelWidth)
                        {
                            num21 = pixelWidth - 1;
                        }
                        for (int num22 = num20; num22 <= num21; num22++)
                        {
                            pixels[(j * pixelWidth) + num22] = color;
                        }
                    }
                }
            }
        }

        public static void FillPolygon(this WriteableBitmap bmp, int[] points, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.FillPolygon(points, num2);
        }

        public static void FillQuad(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int color)
        {
            bmp.FillPolygon(new int[] { x1, y1, x2, y2, x3, y3, x4, y4, x1, y1 }, color);
        }

        public static void FillQuad(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.FillQuad(x1, y1, x2, y2, x3, y3, x4, y4, num2);
        }

        public static void FillRectangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
        {
            int pixelWidth = bmp.PixelWidth;
            int pixelHeight = bmp.PixelHeight;
            int[] pixels = bmp.Pixels;
            if ((((x1 >= 0) || (x2 >= 0)) && ((y1 >= 0) || (y2 >= 0))) && (((x1 < pixelWidth) || (x2 < pixelWidth)) && ((y1 < pixelHeight) || (y2 < pixelHeight))))
            {
                if (x1 < 0)
                {
                    x1 = 0;
                }
                if (y1 < 0)
                {
                    y1 = 0;
                }
                if (x2 < 0)
                {
                    x2 = 0;
                }
                if (y2 < 0)
                {
                    y2 = 0;
                }
                if (x1 >= pixelWidth)
                {
                    x1 = pixelWidth - 1;
                }
                if (y1 >= pixelHeight)
                {
                    y1 = pixelHeight - 1;
                }
                if (x2 >= pixelWidth)
                {
                    x2 = pixelWidth - 1;
                }
                if (y2 >= pixelHeight)
                {
                    y2 = pixelHeight - 1;
                }
                int num3 = y1 * pixelWidth;
                int num4 = num3 + x1;
                int num5 = num3 + x2;
                for (int i = num4; i < num5; i++)
                {
                    pixels[i] = color;
                }
                int count = (x2 - x1) * 4;
                int srcOffset = num4 * 4;
                int num9 = (y2 * pixelWidth) + x1;
                for (int j = num4 + pixelWidth; j < num9; j += pixelWidth)
                {
                    Buffer.BlockCopy(pixels, srcOffset, pixels, j * 4, count);
                }
            }
        }

        public static void FillRectangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.FillRectangle(x1, y1, x2, y2, num2);
        }

        public static void FillTriangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int x3, int y3, int color)
        {
            bmp.FillPolygon(new int[] { x1, y1, x2, y2, x3, y3, x1, y1 }, color);
        }

        public static void FillTriangle(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int x3, int y3, Color color)
        {
            int num = color.A + 1;
            int num2 = (((color.A << 0x18) | (((byte)((color.R * num) >> 8)) << 0x10)) | (((byte)((color.G * num) >> 8)) << 8)) | ((byte)((color.B * num) >> 8));
            bmp.FillTriangle(x1, y1, x2, y2, x3, y3, num2);
        }
    }
}
